Repository navigation
perf: packed arrays write through the leaf cache - #68
Merged
Merged
Conversation
alexvictoor
force-pushed
the
perf/packed-write-leaf-cache
branch
4 times, most recently
from
November 14, 2026 20:08
bdc333c to
b510adb
Compare
add(), increment() and set() used to walk the set trees for every byte they touched (getPackedIndex with insertAsNeeded), and only refreshed the leaf cache afterwards. They now take the leaf of the cached group directly and descend only for sets the cache has no leaf for, so sequential writes (histogram add, decoding, recordValue runs) mostly skip the descents. For the cached group, leafIndexBySet[0 .. knownSets) holds a leaf or NO_LEAF; sets from knownSets upwards are not looked up yet. get(), add() and set() resolve sets from byte 0 upwards without skipping one, so the known sets always form a prefix and evicting a group is a single knownSets = 0 instead of a reset of every entry. A miss, the common case for random recordValue such as latency recordings, therefore costs no more than before the cache: a write looks up only the sets it touches, get() resolves the remaining ones on demand. The write fast path skips getPackedIndex(), which held the only virtualLength check of the packed path, so a cache hit now checks the bound in cacheGroupOf(); a miss leaves it to getPackedIndex(), called for set 0 by every access after a miss. Correctness rests on the same invariants as the read cache: a leaf never moves once created (entries are only appended, resizeArray copies them in place), every write that creates a leaf records it so a known NO_LEAF is always current, and anything rebuilding the context invalidates the cache. The unpacked fallback is unchanged: the fast path only runs on a packed context, and unpack() is still reached from the inserting descents. Both the TypeScript and the AssemblyScript implementations change the same way, with property-based (fast-check) and as-test cases covering interleaved get/add/set/increment within a group, carries across cached and uncached sets, sets to smaller values, out-of-range indexes in a cached group, and the unpacked fallback after cached writes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
alexvictoor
force-pushed
the
perf/packed-write-leaf-cache
branch
from
November 14, 2026 20:12
b510adb to
0b2e54a
Compare
alexvictoor
marked this pull request as ready for review
November 14, 2026 20:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PackedArray.add(),increment()andset()now go through the leaf cache from #63 (onlyget()used it before), in both the TS and the AssemblyScript implementations. The 8 indexes of a group share one leaf per byte set, so writes on nearby indexes skip the set-tree walk (3-5 dependent reads per set) and write directly atleafIndex * 8 + (index & 7).Cache state: for the cached group,
leafIndexBySet[0 .. knownSets)holds a leaf orNO_LEAF; sets fromknownSetsupwards are not looked up yet.get/add/setresolve sets from byte 0 upwards without skipping one, so the known sets are always a prefix and one counter describes them.cacheGroupOf(index) if group != cachedGroup // miss cachedGroup = group - for b in 0..7: leafIndexBySet[b] = UNKNOWN_LEAF // 8 writes + knownSets = 0 // 1 write + // bound checked by getPackedIndex(), always called for set 0 after a miss - check index < virtualLength // every call + else if index >= virtualLength // hit only: no walk to check it + throw add / set / get, for byteNum from 0 upwards - leaf = leafIndexBySet[byteNum] + leaf = byteNum < knownSets ? leafIndexBySet[byteNum] : UNKNOWN_LEAF if leaf < 0 walk set tree (insert for writes, probe for get / set 0) record leaf or NO_LEAF + knownSets = max(knownSets, byteNum + 1) read / write byte at leaf * 8 + (index & 7)Why the counter: latency recordings land on scattered buckets, so a miss is the common case for
recordValue. Resetting 8 entries per miss made random writes up to ~9% slower than master; the counter makes eviction a single write without adding a load on the hit path (a per-entry generation stamp would have).Correctness:
NO_LEAFis always current.setVirtualLength/clearresetcachedGroupandknownSets.add's carry-over into the unpacked context is unchanged.Evidence
Tests:
npm test238 passed,npm run typecheckclean,npm run astest15291/15291.New specs: fast-check properties in
PackedArray.fc.spec.ts(interleavedget/add/increment/seton 4 groups, cached writes while storage fills up and unpacks), AS cases inPackedArrayAccess.test.ts, out-of-bound access after a cached access inPackedArray.spec.ts.Mutations run against the JS specs, each caught:
Benchmarks: A/B/C on the same Apple Silicon machine, 3 alternating rounds, median in M ops/s (higher is better). "latency values" = log-normal around 3 ms in µs, the realistic
recordValueworkload.c6ee2a2)addaddsetset(i, 0)Recording latency values is on par with master in WASM and ~2% faster in JS; sequential writes (
Histogram.add, decoding) keep the 2.9x / +31% gain.set(i, 0)on a bare array stays the one inconclusive row; histograms do not hit it on the recording path.Merge Danger
Door: two-way
Internal to
PackedArray, no API or encoding change; a revert restores the previous behaviour.Blast Radius: packed histograms
A cache bug would return wrong counts for packed histograms (JS and WASM), silently. Covered by the model-based fast-check properties and the mutations above. The prefix rule is an invariant future edits must keep: a code path that resolves a set without resolving the lower ones first would break it, which the class comment states.
🤖 Generated with Claude Code